package iot.operation.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import iot.operation.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import iot.operation.domain.IotDeviceType;
import iot.operation.service.IIotDeviceTypeService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 设备大类Controller
 * 
 * @author lujw
 * @date 2022-06-09
 */
@RestController
@RequestMapping("/DeviceType")
public class IotDeviceTypeController extends BaseController
{
    @Autowired
    private IIotDeviceTypeService iotDeviceTypeService;

    /**
     * 查询设备大类列表
     */
    @RequiresPermissions("iotoperation:DeviceType:list")
    @GetMapping("/list")
    public TableDataInfo list(IotDeviceType iotDeviceType)
    {
        startPage();
        List<IotDeviceType> list = iotDeviceTypeService.selectIotDeviceTypeList(iotDeviceType);
        return getDataTable(list);
    }

    /**
     * 导出设备大类列表
     */
    @RequiresPermissions("iotoperation:DeviceType:export")
    @Log(title = "设备大类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotDeviceType iotDeviceType)
    {
        List<IotDeviceType> list = iotDeviceTypeService.selectIotDeviceTypeList(iotDeviceType);
        ExcelUtil<IotDeviceType> util = new ExcelUtil<IotDeviceType>(IotDeviceType.class);
        util.exportExcel(response, list, "设备大类数据");
    }

    /**
     * 获取设备大类详细信息
     */
    @RequiresPermissions("iotoperation:DeviceType:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(iotDeviceTypeService.selectIotDeviceTypeById(id));
    }

    /**
     * 新增设备大类
     */
    @RequiresPermissions("iotoperation:DeviceType:add")
    @Log(title = "设备大类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IotDeviceType iotDeviceType)
    {
        if (StringUtils.isNotEmpty(iotDeviceType.getTypeCode()) && Constants.NOT_UNIQUE.equals(iotDeviceTypeService.checkTypeCodeUnique(iotDeviceType)))
        {
            return AjaxResult.error("新增设备类型'" + iotDeviceType.getTypeName() + "'失败，类型编码已存在");
        }
        iotDeviceType.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iotDeviceTypeService.insertIotDeviceType(iotDeviceType));
    }

    /**
     * 修改设备大类
     */
    @RequiresPermissions("iotoperation:DeviceType:edit")
    @Log(title = "设备大类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotDeviceType iotDeviceType)
    {
        if (StringUtils.isNotEmpty(iotDeviceType.getTypeCode()) && Constants.NOT_UNIQUE.equals(iotDeviceTypeService.checkTypeCodeUnique(iotDeviceType)))
        {
            return AjaxResult.error("修改设备类型'" + iotDeviceType.getTypeName() + "'失败，类型编码已存在");
        }
        iotDeviceType.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iotDeviceTypeService.updateIotDeviceType(iotDeviceType));
    }

    /**
     * 删除设备大类
     */
    @RequiresPermissions("iotoperation:DeviceType:remove")
    @Log(title = "设备大类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(iotDeviceTypeService.deleteIotDeviceTypeByIds(ids));
    }
}
